package com.itmg.analyze;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.itmg.analyze.jp.url.bean.AmazonSearchIndexBean;
import com.itmg.analyze.thread.KakakuSearchThread;
import com.itmg.analyze.thread.RakutenSearchThread;
import com.itmg.analyze.thread.YahooAuctionSearchThread;
import com.itmg.analyze.thread.YahooSearchThread;
import com.itmg.factory.AnalyzeFactory;
import com.itmg.tools.format.FormatListUtil;
import com.itmg.util.Constants;
import com.itmg.vo.SearchParameterVO;
import com.itmg.vo.SearchResultItemVO;
import com.itmg.vo.SearchResultVO;
import com.itmg.vo.TargetDataVO;

/**
 * 得到商品信息
 */
public class AnalyzeContext {
	
	protected Logger log = Logger.getLogger(AnalyzeContext.class.getName());
	
	public AnalyzeContext() {
	}
	
	public SearchResultVO getSearchResultItemVOList(SearchParameterVO searchParameterVO) throws Exception
	{
		List<SearchResultItemVO> resultList = new ArrayList<SearchResultItemVO>();
		List<SearchResultItemVO> allAmazonList = new ArrayList<SearchResultItemVO>();
		SearchResultVO searchResultVO = new SearchResultVO();
		Integer totalResultNumber = 0;
		String isAuction = searchParameterVO.getIsAuction();
		
		//启动kakaku线程
		TargetDataVO kakakudata = AnalyzeFactory.getKakakuURLAnlyze().getURLDataByParameter(searchParameterVO);
		KakakuSearchThread kakakuThread = new KakakuSearchThread(kakakudata);
		Thread threadKakaku = new Thread(kakakuThread);
		threadKakaku.start();
log.info("kakakuThread start" + threadKakaku.getName());	

		//启动rekuten线程
		TargetDataVO rakutendata = AnalyzeFactory.getRakutenURLAnlyze().getURLDataByParameter(searchParameterVO);
		RakutenSearchThread rakutenThread = new RakutenSearchThread(rakutendata);
		Thread threadRakuten = new Thread(rakutenThread);
		threadRakuten.start();
log.info("rakutenThread start" + threadRakuten.getName());	

		RakutenSearchThread rakutenAuctionThread = null;
		Thread threadRakutenAuction = null;
		YahooAuctionSearchThread yahooAuctionThread = null;
		Thread threadYahooAuction = null;
		if(isAuction == null || "".equals(isAuction))
			isAuction = "0";
		if(Constants.ISAUCTION_DEFAULT.equals(isAuction)){
			//启动rekutenAuction线程
			TargetDataVO rakutenAuctiondata = AnalyzeFactory.getRakutenAuctionURLAnlyze().getURLDataByParameter(searchParameterVO);
			rakutenAuctionThread = new RakutenSearchThread(rakutenAuctiondata);
			threadRakutenAuction = new Thread(rakutenAuctionThread);
			threadRakutenAuction.start();
	log.info("rakutenAuctionThread start" + threadRakutenAuction.getName());
	
			//启动yahooAuction线程
			TargetDataVO yahooAuctionsdata = AnalyzeFactory.getYahooAuctionsURLAnlyze().getURLDataByParameter(searchParameterVO);
			yahooAuctionThread = new YahooAuctionSearchThread(yahooAuctionsdata);
			threadYahooAuction = new Thread(yahooAuctionThread);
			threadYahooAuction.start();
	log.info("yahooAuctionThread start" + threadYahooAuction.getName());
		}

		//启动yahoo线程
		TargetDataVO yahoodata = AnalyzeFactory.getYahooURLAnlyze().getURLDataByParameter(searchParameterVO);
        YahooSearchThread yahooThread = new YahooSearchThread(yahoodata);
        Thread threadYahoo = new Thread(yahooThread);
        threadYahoo.start();
log.info("yahooThread start" + threadYahoo.getName());		

//获得数据
		//得到Kakaku中的商品，数量自己设定
		threadKakaku.join();
		SearchResultVO searchResultVO_kakakudata = kakakuThread.getKakakuVO();
		List<SearchResultItemVO> kakakuList = searchResultVO_kakakudata.getResultItemList();
		if(kakakuList != null){
			resultList.addAll(kakakuList);
			log.info("kakakuList total size ==> "+kakakuList.size());
		}
			
		totalResultNumber += searchResultVO_kakakudata.getTotalResultNumber();

		//得到Rakuten中的商品，数量自己设定
		threadRakuten.join();
		SearchResultVO searchResultVO_rakutendata = rakutenThread.getKakakuVO();
		List<SearchResultItemVO> rakutendataList = searchResultVO_rakutendata.getResultItemList();
		if(rakutendataList != null){
			resultList.addAll(rakutendataList);
			log.info("rakutendataList total size ==> "+rakutendataList.size());
		}
		totalResultNumber += searchResultVO_rakutendata.getTotalResultNumber();

		//得到Yahoo中的商品，数量自己设定
		threadYahoo.join();
		SearchResultVO searchResultVO_yahoodata = yahooThread.getKakakuVO();
		List<SearchResultItemVO> yahoodataList = searchResultVO_yahoodata.getResultItemList();
		if(yahoodataList != null){
			resultList.addAll(yahoodataList);
			log.info("yahoodataList total size ==> "+yahoodataList.size());
		}
		totalResultNumber += searchResultVO_yahoodata.getTotalResultNumber();

		if(Constants.ISAUCTION_DEFAULT.equals(isAuction)){
			//得到YahooAuction中的商品
			threadYahooAuction.join();
			SearchResultVO searchResultVO_yahooAuctionsdata = yahooAuctionThread.getKakakuVO();
			List<SearchResultItemVO> yahooAuctionsdataList = searchResultVO_yahooAuctionsdata.getResultItemList();
			if(yahooAuctionsdataList != null){
				resultList.addAll(yahooAuctionsdataList);
				log.info("yahooAuctionsdataList total size ==> "+yahooAuctionsdataList.size());
			}
			totalResultNumber += searchResultVO_yahooAuctionsdata.getTotalResultNumber();
	
			//得到RakutenAuction中的商品，数量自己设定
			threadRakutenAuction.join();	
			SearchResultVO searchResultVO_rakutenAuctiondata = rakutenAuctionThread.getKakakuVO();
			List<SearchResultItemVO> rakutenAuctiondataList = searchResultVO_rakutenAuctiondata.getResultItemList();
			if(rakutenAuctiondataList != null){
				resultList.addAll(rakutenAuctiondataList);
				log.info("rakutenAuctiondataList total size ==> "+rakutenAuctiondataList.size());
			}
			totalResultNumber += searchResultVO_rakutenAuctiondata.getTotalResultNumber();
		}

		//得到Amazon中的商品，每次得10件，且分为不同的类型
		List<String> list = AmazonSearchIndexBean.getAmazonType(searchParameterVO.getCategory());
		for(int i=0;i<list.size();i++){
			String searchIndex = list.get(i);
			searchParameterVO.setSearchIndex(searchIndex);
			TargetDataVO amazondata = AnalyzeFactory.getAmazonURLAnlyze().getURLDataByParameter(searchParameterVO);
			SearchResultVO searchResultVO_amazondata = AnalyzeFactory.getAmazonDataAnlyze().getSearchResultItemVO(amazondata);
			List<SearchResultItemVO> amazonList = searchResultVO_amazondata.getResultItemList();
			if(amazonList != null)
				allAmazonList.addAll(amazonList);
			totalResultNumber += searchResultVO_amazondata.getTotalResultNumber();
		}
		//allAmazonList拿出20条数据放在resultList里面
		List<SearchResultItemVO> listResult = AmazonSearchIndexBean.confusionOrder(allAmazonList);
		resultList.addAll(listResult);
log.info("amazonList total size ==> "+allAmazonList.size());
		
        //将没有价格的商品放到最后面
		List<SearchResultItemVO> itemList = FormatListUtil.getFormatList(resultList);
		searchResultVO.setResultItemList(itemList);
		searchResultVO.setTotalResultNumber(totalResultNumber);
log.info("resultList total size ==> "+resultList.size());
	    return searchResultVO;
	} 
}
